﻿using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using MongoDB.Driver;
using System;
using System.Linq;
using System.Windows;
using System.Configuration;

namespace LoginExample
{
    public partial class MainWindow : Window
    {
        private IMongoCollection<User> _usersCollection;

        public MainWindow()
        {
            InitializeComponent();

            this.Width = 600;
            this.Height = 400;

            // 从 App.config 中读取 MongoDB 连接字符串
            string connectionString = ConfigurationManager.ConnectionStrings["MongoDBConnection"].ConnectionString;

            // 使用读取的连接字符串连接 MongoDB
            var client = new MongoClient(connectionString);
            var database = client.GetDatabase("LoginDB");
            _usersCollection = database.GetCollection<User>("Users");

            // 加载之前保存的用户名和密码（如果记住密码被选中）
            if (Properties.Settings.Default.RememberMe)
            {
                txtUsername.Text = Properties.Settings.Default.Username;
                txtPassword.Password = Properties.Settings.Default.Password;
                chkRememberMe.IsChecked = true; // 确保复选框被勾选
            }
            else
            {
                chkRememberMe.IsChecked = false; // 如果没有记住密码，确保复选框为未选中
            }
        }

        private void Login_Click(object sender, RoutedEventArgs e)
        {
            string username = txtUsername.Text;
            string password = txtPassword.Password;

            // 输入检查
            if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
            {
                lblMessage.Text = "请输入用户名和密码";
                return;
            }

            try
            {
                // 查询用户名是否存在
                var user = _usersCollection.Find(u => u.Username == username).FirstOrDefault();

                if (user == null)
                {
                    lblMessage.Text = "用户名不存在";
                }
                else if (user.Password == password)
                {
                    lblMessage.Text = "登录成功！";
                    lblMessage.Foreground = System.Windows.Media.Brushes.Green;

                    // 打开主页
                    HomePage homePage = new HomePage();
                    homePage.Show();

                    // 如果选择记住密码，保存用户名和密码
                    if (chkRememberMe.IsChecked == true)
                    {
                        Properties.Settings.Default.Username = username;
                        Properties.Settings.Default.Password = password;
                        Properties.Settings.Default.RememberMe = true;
                        Properties.Settings.Default.Save(); // 保存设置
                    }
                    else
                    {
                        // 如果没有选择记住密码，则清除保存的信息
                        Properties.Settings.Default.Username = string.Empty;
                        Properties.Settings.Default.Password = string.Empty;
                        Properties.Settings.Default.RememberMe = false;
                        Properties.Settings.Default.Save(); // 保存设置
                    }

                    this.Close();
                }
                else
                {
                    lblMessage.Text = "密码错误";
                    lblMessage.Foreground = System.Windows.Media.Brushes.Red;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"数据库连接失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
